package com.moshang.blog.service.impl;

import com.moshang.blog.core.constant.Constants;
import com.moshang.blog.core.constant.CurrentUser;
import com.moshang.blog.entity.QETag;
import com.moshang.blog.entity.Rescource;
import com.moshang.blog.entity.SysDict;
import com.moshang.blog.entity.UploadInfo;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.service.SysDictService;
import com.moshang.blog.service.SysRescourceService;
import com.moshang.blog.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

/**
 * @program: blog
 * @description: ${description}
 * @author: xieweiwei
 * @create: 2018-09-30 16:38
 **/
@Service("localUploadService")
public class LocalUploadServiceImpl implements UploadService {
    @Value("${upload-path}")
    private String uploadpath;
    @Autowired
    private SysRescourceService rescourceService;
    @Autowired
    private SysDictService sysDictService;
    @Override
    public String upload(MultipartFile file, Integer status) throws IOException, NoSuchAlgorithmException {
        CurrentUser user= MySysUser.currentUser();
        byte[] data = file.getBytes();
        QETag tag = new QETag();
        String hash = tag.calcETag(file);
        String source="local";
        Rescource rescource = rescourceService.selectByCondition(hash,source);
        if( rescource!= null){
            return rescource.getWebUrl();
        }
        String extName = file.getOriginalFilename().substring(
                file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID() + extName;
        String contentType = file.getContentType();
        StringBuffer sb = new StringBuffer(ResourceUtils.getURL("classpath:").getPath());
        String filePath = sb.append("static/upload/").toString();
        File targetFile = new File(filePath);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        out.write(data);
        out.flush();
        out.close();
        String webUrl = "static/upload/"+fileName;
        rescource = new Rescource();
        rescource.setFileName(fileName);
        rescource.setFileSize(new java.text.DecimalFormat("#.##").format(file.getSize()/1024)+"kb");
        rescource.setHash(hash);
        rescource.setFileType(contentType);
        rescource.setWebUrl(webUrl);
        rescource.setSource("local");
        rescource.setCreateBy(user.getId());
        rescource.setCreateTime(new Date());
        rescource.setUpdateBy(user.getId());
        rescource.setUpdateTime(new Date());
        rescource.setIsActivited(0);
        if (status==0) {
            SysDict sysDict = sysDictService.selectByCondition("文件分类", Constants.DEFAULT_CATEGORY);
            if (sysDict!=null){
                rescource.setCategory(sysDict.getId());
            }
        }
        rescourceService.insertRescource(rescource);
        return webUrl;
    }

    @Override
    public Boolean testAccess(UploadInfo uploadInfo) {
        return null;
    }

    @Override
    public void download(String fileName, String remoteName)throws IOException {
    }
}
